#include"STC15WXX.h"
#define YELLOW_BAT P31//定义黄灯 状态指示灯
#define LED_WORK_STA P30//宏定义  位定义   红灯（工作指示灯）
//sbit LED_WORK_STA=P3 ^ 0; //与上面相同
#define STA_DOOR_OPEN P35//KAI
#define  STA_DOOR_CLOSE P34//GUAN
#define BAT_LAW P34

sbit INT1 = 0xB3;
void System_Init(void);
unsigned char flag_Door;//=0无动作 1关门 2开门
unsigned char DoorSta;//=0无动作 1关门 2开门 门状态
unsigned char flag_VlotLow;//=0电池未低压  =1电池到低压状态  =2电池处于低压状态

void Delay10ms()		//@5.5296MHz
{
	unsigned char i, j;
	do
	{
		while (--j);
	} while (--i);
}
void Delay100ms()		//@5.5296MHz
{
	unsigned char i, j, k;
	i = 3;
	j = 26;
	k = 223;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void main()
{
    void System_Init(void);
    while(1)
    {
        if(flag_Door==1)
        {//门关闭
            flag_Door = 0;
            Delay10ms();
            if(STA_DOOR_CLOSE==0)//门关闭了  ==0是下降沿 低电平有效
            {
                DoorSta = 1;
            }
        }
        else if(flag_Door==2)
        {//门打开
            flag_Door = 0;
            Delay10ms();
            if(STA_DOOR_OPEN==0)//门打开了
            {
                DoorSta = 2;
            }

        }
        else
        {
            flag_Door = 0;//无动作
        }

        if(flag_VlotLow)//相当于flag_VlotLow！=0处于低压状态时
        {
            if(flag_VlotLow==1)//进入低压瞬间
            {
                Delay10ms();
                if(BAT_LAW==0)//下降沿触发 低电平有效
                {
                    flag_VlotLow =2;//处于低压状态
                }
                else//如果没进入低压状态
                {
                    flag_VlotLow =0;
                }
            }
        }

        if(DoorSta)//相当于DoorSta！=0
        {
            LED_WORK_STA = 1;//工作状态灯打开
            Delay100ms();
            LED_WORK_STA = 0;//工作状态灯关闭

            DoorSta = 0;//状态复位
        }

        if(flag_VlotLow =2)//当进入低压状态后
        {//电池低压 黄灯提示
            YELLOW_BAT = 1;//工作状态灯打开
            Delay100ms();
            YELLOW_BAT = 0;//工作状态灯关闭
        }
    }
}

void System_Init(void)
{
    //寄存器初始化
    P0M0 = 0x00;
    P0M1 = 0x00;
    P1M0 = 0x00;
    P1M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    P3M0 = 0x00;
    P3M1 = 0x00;
    P4M0 = 0x00;
    P4M1 = 0x00;
    P5M0 = 0x00;
    P5M1 = 0x00;
    P6M0 = 0x00;
    P6M1 = 0x00;
    P7M0 = 0x00;
    P7M1 = 0x00;

    //INT1 中断
    INT1 = 1;
    IT1 = 1;                    //设置INT1的中断类型 (1:仅下降沿 0:上升沿和下降沿)
    EX1 = 1;                    //使能INT1中断
    EA = 1;
    //INT2 中断
    INT_CLKO |= 0x10;               //(EX2 = 1)使能INT2中断
    //INT3 中断
    INT_CLKO |= 0x20;               //(EX3 = 1)使能INT3中断
    EA = 1;


    flag_Door = 0;//初始化门状态
    DoorSta = 0;
    flag_VlotLow = 0;

    //推挽输出
    P3M1 &= 0xFC;
    P3M0 |= 0x03;

    P3M0 |= 0x30;//P34 P35为开漏
    P3M1 |= 0x30;

    LED_WORK_STA = 0;//先把灯拉低关闭
    YELLOW_BAT = 0;
    //P30 = 1;
    //P31 = 1;
}

//中断1 低电压检测
void exint1() interrupt 2       //INT1中断入口
{
    
}

//中断2 关门检测
void exint2() interrupt 10          //INT2中断入口
{
    flag_Door = 1;//关门
}

//中断3 开门检测
void exint3() interrupt 11          //INT3中断入口
{
    flag_Door = 2;//开门
}

